package com.ww.wwojbackendjudgeservice.judge.codsandbox.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.ww.wwojbackendcommon.common.ErrorCode;
import com.ww.wwojbackendcommon.exception.BusinessException;
import com.ww.wwojbackendjudgeservice.judge.codsandbox.CodeSandbox;
import com.ww.wwojbackendmodel.codesandbox.ExecuteCodeRequest;
import com.ww.wwojbackendmodel.codesandbox.ExecuteCodeResponse;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 15357
 * 远程代码沙箱（实际调用接口的代码沙箱）
 */
public class RemoteCodeSandbox implements CodeSandbox {

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        System.out.println("远程代码沙箱");
        String url = "http://159.75.105.171:8090/executeCode";
        String json = JSONUtil.toJsonStr(executeCodeRequest);
        String responseStr = HttpUtil.createPost(url)
                .body(json)
                .header("wwoj-codesandbox-auth-by-ww", "$W$~vrZ&L!ht^U%fF2erwerzZzHTjWSwY%@ZeEJ^*(qZ()D3npx")
                .execute()
                .body();
        if (StringUtils.isBlank(responseStr)) {
            throw new BusinessException(ErrorCode.API_REQUEST_ERROR, "executeCode remote error, message = " + responseStr);
        }
        return JSONUtil.toBean(responseStr, ExecuteCodeResponse.class);
    }
}
